﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata.Util;
using Kingdee.BOS.ServiceHelper;
using Kingdee.K3.FIN.App.Core.Match.Object;
using LiMinCRMPlugin.InterfaceDocking;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{
    // <summary>
    /// CRM同步的发货通知和应收单，订单信息有缺失，联查更新当天所有发货通知和应收单这部分缺失字段
    /// </summary>
    /// <remarks>
    /// 功能：CRM同步的发货通知和应收单，订单信息有缺失，联查更新当天所有发货通知和应收单这部分缺失字段
    /// 创建人：wzx
    /// 创建日期：2024年5月13日
    /// </remarks>
    [Description("更新缺失的订单信息")]
    [Kingdee.BOS.Util.HotUpdate]
    public class DeliveryAndReceivableUpdatePlan : IScheduleService
    {
        public void Run(Context ctx, Schedule schedule)
        {
            #region 发货通知
            var sqlDelivery = @"/*dialect*/select b.FID,b.FENTRYID from T_SAL_DELIVERYNOTICE a
inner join T_SAL_DELIVERYNOTICEENTRY b on a.FID=b.FID
where fbillno='1012FHTZ2400017' and F_DEV_DataSrc='CRM'";
//FCreateDate=CONVERT(date,GETDATE()) and F_DEV_DataSrc='CRM'";
            DataTable dt_Delivery = DBUtils.ExecuteDataSet(ctx, sqlDelivery).Tables[0];
            foreach (DataRow itemD in dt_Delivery.Rows)
            {
                int FID = Convert.ToInt32(itemD["FID"]);
                int FENTRYID = Convert.ToInt32(itemD["FENTRYID"]);
                var UpdateDeENTRY = string.Format(@"/*dialect*/update T_SAL_DELIVERYNOTICEENTRY set FOrderSeq=(select FSeq from T_SAL_ORDERENTRY 
where FENTRYID=(select FSID from T_SAL_DELIVERYNOTICEENTRY_LK where FENTRYID='{0}'))
where FENTRYID='{0}'", FENTRYID);
                DBUtils.Execute(ctx, UpdateDeENTRY);

                var UpdateDeENTRY_E = string.Format(@"/*dialect*/update T_SAL_DELIVERYNOTICEENTRY_E set FSOEntryId=(select FSID from T_SAL_DELIVERYNOTICEENTRY_LK where FENTRYID='{0}') where FENTRYID='{0}'", FENTRYID);
                DBUtils.Execute(ctx, UpdateDeENTRY_E);
            }
            #endregion

            #region 应收单
            var sqlReceivable = @"/*dialect*/select b.FID,b.FENTRYID from t_AR_receivable a
inner join t_AR_receivableEntry b on a.FID=b.FID
where fbillno='1012AR2400034'  and F_DEV_DataSrc='CRM' ";

            //where FCreateDate = CONVERT(date, GETDATE()) and F_DEV_DataSrc = 'CRM' ";
            DataTable dt_Receivable = DBUtils.ExecuteDataSet(ctx, sqlReceivable).Tables[0];
            foreach (DataRow itemR in dt_Receivable.Rows)
            {
                int FID = Convert.ToInt32(itemR["FID"]);
                int FENTRYID = Convert.ToInt32(itemR["FENTRYID"]);
                var UpdateReENTRY = string.Format(@"/*dialect*/update t_AR_receivableEntry set FORDERNUMBER=(select F_DEV_OrderNo from t_AR_receivable where FID='{0}'),
FORDERENTRYSEQ=(select FSeq from T_SAL_ORDERENTRY 
where FENTRYID=(select FSOEntryId from T_SAL_DELIVERYNOTICEENTRY_E
where FENTRYID=(select FSID from T_SAL_OUTSTOCKENTRY_LK 
where FENTRYID=(select FSID from t_AR_receivableEntry_LK where FENTRYID=(select FSID from t_AR_receivableEntry_LK where FENTRYID='{1}'))))),
FORDERENTRYID=(select FSOEntryId from T_SAL_DELIVERYNOTICEENTRY_E
where FENTRYID=(select FSID from T_SAL_OUTSTOCKENTRY_LK 
where FENTRYID=(select FSID from t_AR_receivableEntry_LK where FENTRYID=(select FSID from t_AR_receivableEntry_LK where FENTRYID='{1}')))),
FSOURCEBILLTYPEID=(select FBillTypeID from t_AR_receivable where FID=(select FSBILLID from t_AR_receivableEntry_LK where FENTRYID='{1}'))
where FENTRYID='{1}'", FID,FENTRYID);
                DBUtils.Execute(ctx, UpdateReENTRY);
            }
            #endregion


        }
    }
}
